package com.wayne.blog.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Sa-Token 配置类
 */
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    /**
     * 注册 Sa-Token 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，只拦截 /api 开头的接口
        registry.addInterceptor(new SaInterceptor(handle -> {
            SaRouter.match("/api/**")
                    .notMatch("/api/users/*")
                    .notMatch("/api/articles/**")
                    .notMatch("/api/categories/**")
                    .notMatch("/api/tags/**")
                    .notMatch("/api/swagger-ui/**")
                    .notMatch("/api/v3/api-docs/**")
                    .notMatch("/api/doc.html")
                    .check(r -> {
                        StpUtil.checkLogin();
                    });
        })).addPathPatterns("/api/**");
    }
} 